;;; +function.el --- configuration -*- lexical-binding: t -*-

;;; Commentary:

;;; Code:

;; -------- select block plus one blankline --------
(defun cor5-select-block ()
  "Select the current/next block plus 1 blankline.
If region is active, extend selection downward by block.

URL `http://xahlee.info/emacs/emacs/emacs_select_text_block.html'
Version: 2019-12-26 2021-08-13 2023-11-14"
  (interactive)
  (if (region-active-p)
      (re-search-forward "\n[ \t]*\n[ \t]*\n*" nil :move)
    (progn
      (skip-chars-forward " \n\t")
      (when (re-search-backward "\n[ \t]*\n" nil :move)
        (goto-char (match-end 0)))
      (push-mark (point) t t)
      (re-search-forward "\n[ \t]*\n" nil :move))))

(global-set-key (kbd "M-h") 'cor5-select-block)

;; -------- delete empty lines --------
;; @mayingjun via emacs-cn
(defun cor5-delete-empty-line (beg end)
  "Delete empty lines if selected, otherwise the whole buffer."
  (interactive "r")
  (let ((empty-line "^[\t  ]*$"))
    (save-excursion
      (if (region-active-p)
          (flush-lines empty-line beg end)
        (flush-lines empty-line (point-min) (point-max))))))

(defalias 'cor5-delete-blank-line 'cor5-delete-empty-line)

;; -------- open configure files --------
;; for Emacs config init.el
(defun cor5-open-init-file ()
  "Open Emacs `init.el' file."
  (interactive)
  (find-file (expand-file-name "init.el" user-emacs-directory)))

(global-set-key (kbd "C-c E") #'cor5-open-init-file)

;; for RIME config default.custom.yaml
(defun cor5-open-rime-custom-file ()
  "Open RIME default.custom.yaml config file."
  (interactive)
  (let ((fcitx5-custom "~/.local/share/fcitx5/rime/default.custom.yaml")
        (fcitx-custom "~/.config/fcitx/rime/default.custom.yaml"))
    (if (file-exists-p fcitx5-custom)
        (find-file fcitx5-custom)
      (when (file-exists-p fcitx-custom)
        (find-file fcitx-custom)))))

(global-set-key (kbd "C-c I") #'cor5-open-rime-custom-file)

;; -------- Rename buffer & file name --------
;; source: http://steve.yegge.googlepages.com/my-dot-emacs-file
(defun cor5-rename-current-file-and-buffer (new-name)
  "Renames both current buffer and file it's visiting to NEW-NAME."
  (interactive "sNew name: ")
  (let ((name (buffer-name))
        (filename (buffer-file-name)))
    (if (not filename)
        (message "Buffer '%s' is not visiting a file!" name)
      (if (get-buffer new-name)
          (message "A buffer named '%s' already exists!" new-name)
        (progn
          (rename-file filename new-name 1)
          (rename-buffer new-name)
          (set-visited-file-name new-name)
          (set-buffer-modified-p nil))))))

;; -------- Switch to scratch --------
(defun cor5-switch-to-scratch ()
  "Switch to scratch butter."
  (interactive)
  (switch-to-buffer "*scratch*"))

(global-set-key (kbd "<f9>") #'cor5-switch-to-scratch)

;; -------- Switch to last buffer --------
(defun cor5-last-buffer (arg)
  "Switch to last buffer.
Argument ARG if not nil, switching in a new window."
  (interactive "P")
  (cond
   ((minibufferp)
    (keyboard-escape-quit))
   ((not arg)
    (mode-line-other-buffer))
   (t
    (split-window)
    (mode-line-other-buffer))))

(global-set-key (kbd "<escape>") #'cor5-last-buffer)

;; -------- revert current buffer as sudo --------
(defun cor5-reopen-file-with-sudo ()
  "Open a file with root permission."
  (interactive)
  (find-alternate-file (format "/sudo::%s" (buffer-file-name))))

(provide '+function)

;;; +function.el ends here
